package Data;

import android.os.Environment;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ErrorLoger {
    public  static void Log(Exception ex)
    {
        String text=ex.getMessage()+"\n";
        for (int i=0;i<ex.getStackTrace().length;i++)
            text+=ex.getStackTrace()[i].getMethodName()+"出错！   在第"+String.valueOf( ex.getStackTrace()[i].getLineNumber())+"行\n";
        new ErrorLoger().writeFileData(text);
    }

    String path;
    File file=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()+"/log.txt");
    void Init()
    {
        System.out.println(file.getAbsolutePath());
        if(!file.exists())
        {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
     void writeFileData(String content){
        Init();
        try {
            RandomAccessFile raf = null;
            raf = new RandomAccessFile(file, "rwd");
            try {
                raf.seek(file.length());
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                raf.write(content.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                raf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

}
